package com.example.jl.bethinner.activity;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.dpizarro.uipicker.library.picker.PickerUI;
import com.dpizarro.uipicker.library.picker.PickerUISettings;
import com.example.jl.bethinner.R;
import com.example.jl.bethinner.base.BaseActivity;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class PickUIActivity extends BaseActivity {
    private int currentPosition = -1;
    PickerUI mPickerUI;
    private Button button;
    private String valueResultString="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pick_ui);
        mPickerUI = (PickerUI) findViewById(R.id.picker_ui_view);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if ("点点点".equals(button.getText().toString()))
                   button.setText("确认");
                else {
                   button.setText("点点点");
                   Toast.makeText(PickUIActivity.this, valueResultString, Toast.LENGTH_SHORT).show();
               }
                    if(currentPosition==-1) {
                        mPickerUI.slide();
                    }
                    else{
                        mPickerUI.slide(currentPosition);
                    }


            }
        });

        List<String> options = Arrays.asList(getResources().getStringArray(R.array.months));

        PickerUISettings pickerUISettings = new PickerUISettings.Builder()
                .withItems(options)
                .withBackgroundColor(getRandomColor())
                .withAutoDismiss(false)
                .withItemsClickables(false)
                .withUseBlur(false)
                .build();

        mPickerUI.setSettings(pickerUISettings);
        mPickerUI.setOnClickItemPickerUIListener(
                new PickerUI.PickerUIItemClickListener() {

                    @Override
                    public void onItemClickPickerUI(int which, int position, String valueResult) {
                        //在这里只能坐下保存，不能直接处理之后的事情，要在PickUi消失之后再处理相关的事件
                        currentPosition = position;
                        valueResultString=valueResult;

                    }
                });

    }
    private int getRandomColor() {
        // generate the random integers for r, g and b value
        Random rand = new Random();
        int r = rand.nextInt(255);
        int g = rand.nextInt(255);
        int b = rand.nextInt(255);
        return Color.rgb(r, g, b);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_pick_ui, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
